package com.crm.user.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.crm.base.dao.BaseDao;
import com.crm.sys.dao.SysDao;
import com.crm.sys.model.Role;
import com.crm.sys.model.UserRefRole;
import com.crm.user.dao.UserDao;
import com.crm.user.model.UserAccount;
import com.crm.user.model.UserInfo;

@Repository("userDao")
public class UserDaoImpl extends BaseDao implements UserDao {
	
	private static final String MAPPER_NAMESAPCE = "UserMapper.";

    private static final String MAPPER_NAMESAPCEINFO = "UserInfoMapper.";
    @Autowired
    private SysDao sysDao;
	
	@Override
	public UserAccount getUserAccountInfo(UserAccount user) {
		/*StringBuffer sql = new StringBuffer();
		sql.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_getUserAccountInfo")).append(XmlReader.readXml(Constants.SQLFileName,"UserDao_getUserAccountInfo_param"));
		List<UserAccount> list =this.jdbcTemplate.query(sql.toString(), new Object[]{user.getAccount(),user.getPassword()},new BeanPropertyRowMapper(UserAccount .class));
		return (list!=null&&list.size()>0)?list.get(0):null;*/
		return selectOne(MAPPER_NAMESAPCE+"selectUserAccount", user);
	}
	
	@Override
	public UserAccount getUserAccountByAccount(UserAccount user) {
	/*	
		StringBuffer sql = new StringBuffer();
		try{
			sql.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_getUserAccountInfo")).append("	").append(XmlReader.readXml(Constants.SQLFileName,"UserDao_getUserAccountInfoByAccount_param"));
		}catch(Exception e){
			e.printStackTrace();
		}
		
		List<UserAccount> list =this.jdbcTemplate.query(sql.toString(), new Object[]{account},new BeanPropertyRowMapper(UserAccount .class));
		return (list!=null&&list.size()>0)?list.get(0):null;*/
		return selectOne(MAPPER_NAMESAPCE+"selectUserAccountByAccount", user);
		
	}

	@Override
	public UserAccount insertUser(UserAccount user) {
		/*   */
		/*final  String sql = XmlReader.readXml(Constants.SQLFileName,"UserDao_insertUser");
		KeyHolder keyHolder = new GeneratedKeyHolder();  
		//新增返回主键ID
	    jdbcTemplate.update(new PreparedStatementCreator() {  
	        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {  
	               PreparedStatement ps = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);  
	               ps.setString(1, user.getAccount());  
	               ps.setString(2, user.getPassword());     
	               return ps;  
	        }  
	    }, keyHolder);  
	    Long generatedId = keyHolder.getKey().longValue();   
	    user.setId(generatedId);*/
	    
	    insert(MAPPER_NAMESAPCE+"insert", user);
	    return user ; 
	}

	 
	@Override
	public UserInfo insertUserInfo(final UserInfo userinfo) {
		/*final  String sql = XmlReader.readXml(Constants.SQLFileName,"UserDao_insertUserInfo");
		KeyHolder keyHolder = new GeneratedKeyHolder();  
		//新增返回主键ID
	    jdbcTemplate.update(new PreparedStatementCreator() {  
	        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {  
	               PreparedStatement ps = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);  
	               ps.setLong(1, userinfo.getAid());  
	               ps.setString(2, userinfo.getCreateuser());     
	               ps.setString(3, userinfo.getCreateuser());     
	               return ps;  
	        }  
	    }, keyHolder);  
	    Long generatedId = keyHolder.getKey().longValue();   
	    userinfo.setId(generatedId);*/
		 insert(MAPPER_NAMESAPCEINFO+"insertBase", userinfo);
	    return userinfo ; 
	}

	@Override
	public UserInfo getUserInfo(UserInfo info) {
		/*StringBuffer sql = new StringBuffer();
		sql.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_getUserInfo"));
		List<UserInfo> list =this.jdbcTemplate.query(sql.toString(), new Object[]{id},new BeanPropertyRowMapper(UserInfo .class));
		return (list!=null&&list.size()>0)?list.get(0):null;*/
		return selectOne(MAPPER_NAMESAPCEINFO+"selectUserInfoByUid", info);
	}

	@Override
	public int updateUserInfo(UserInfo userinfo) {
		/*StringBuffer sql = new StringBuffer();
		sql.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_updateUserInfo"));
		int result = jdbcTemplate.update(sql.toString(),new Object[]{
			userinfo.getName(),userinfo.getSex(),userinfo.getBirthday(),userinfo.getCardid(),
			userinfo.getAddress(),userinfo.getSchool(),userinfo.getEducation(),userinfo.getCurrentaddr(),
			userinfo.getPhone(),userinfo.getContactuser(),userinfo.getCuphone(),userinfo.getEmail(),
			userinfo.getRemark(),userinfo.getDeptid(),userinfo.getUpdateuser(),userinfo.getId()
		});
		return result;*/
		
		return update(MAPPER_NAMESAPCEINFO+"updateInfo", userinfo);
	}

	@Override
	public UserAccount getUserAccountInfoById(UserAccount user) {
		/*StringBuffer sql = new StringBuffer();
		sql.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_getUserAccountInfo")).append(XmlReader.readXml(Constants.SQLFileName,"UserDao_getUserAccountInfoById_param"));
		List<UserAccount> list =this.jdbcTemplate.query(sql.toString(), new Object[]{id},new BeanPropertyRowMapper(UserAccount .class));
		return (list!=null&&list.size()>0)?list.get(0):null;*/
		return selectOne(MAPPER_NAMESAPCE+"selectUserAccountById", user);
	}

	@Override
	public int updatePassword(UserAccount useraccount) {
		
		/*StringBuffer sql = new StringBuffer();
		sql.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_updatePassword"));
		int result = jdbcTemplate.update(sql.toString(),new Object[]{
			useraccount.getPassword(),useraccount.getId()
		});
		return result;*/
		return update(MAPPER_NAMESAPCE+"updPassword", useraccount);
	}

	@Override
	public List<UserInfo> getUserList(UserInfo user) {
		/*StringBuffer sql = new StringBuffer();
		StringBuffer sql1 = new StringBuffer();
		sql.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_getUserList"));
		sql1.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_getUserListCount"));
		//将参数列表转换成数组
		List  params = new ArrayList();
		if(StringUtils.isNotEmpty(user.getKeyWord())){
			sql.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_getUserList_param2"));
			sql1.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_getUserList_param2"));
			params.add(user.getKeyWord());
		}
		if(null!=user.getDeptid()&&user.getDeptid()>0){
			sql.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_getUserList_param1"));
			sql1.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_getUserList_param1"));
			params.add(user.getDeptid()); 
		}
		sql.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_getUserList_param4"));
		 List<Object> queryParams= new ArrayList();
		 queryParams.addAll(params);
		 queryParams.add(getBegin(pageNo, pageSize));
		 queryParams.add(pageSize);
		List<UserInfo> list =jdbcTemplate.query(sql.toString() , (Object[])queryParams.toArray(new Object[queryParams.size()]) ,new BeanPropertyRowMapper(UserInfo .class));
		if(list!=null&&list.size()>0){
			int  counts = (Integer) jdbcTemplate.queryForObject(sql1.toString(),(Object[])params.toArray(new Object[params.size()]) ,Integer.class);
			list.get(0).setTotalResults(counts);
		}
		return list;*/
		
		List<UserInfo> list = selectList(MAPPER_NAMESAPCEINFO+"selectList",user);
		if (list != null && list.size()>0) {
			list.get(0).setTotalResults((Integer)selectOne(MAPPER_NAMESAPCEINFO+"selectListCount", user));
		}
		return list;
		
		
	}

	@Override
	public List<Role> getRole(Role role) {
		/*StringBuffer sql = new StringBuffer();
		sql.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_getRole"));
		List<Role> list =jdbcTemplate.query(sql.toString() ,new BeanPropertyRowMapper(Role .class));*/
		return sysDao.getRoleList(null);
	}

	@Override
	public int deleteRoleList(UserAccount useraccount) {
		
		/*StringBuffer sql= new  StringBuffer();
		sql.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_deleteRoleList"));
		int result =jdbcTemplate.update(sql.toString() ,new Object[]{
			uid
			});
		return result;*/
		
		return delete(MAPPER_NAMESAPCE+"delUserRole", useraccount);
	}

	@Override
	public int insertRoleList(UserRefRole role) {
		
		/*StringBuffer sql = new StringBuffer();
		sql.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_insertRoleList"));
		int result =  jdbcTemplate.update(sql.toString(), new Object[] {
			rid,uid
		});
		return result;*/
		return insert(MAPPER_NAMESAPCE+"insertUserRole",role);
	}

	@Override
	public UserInfo getUserByid(UserInfo info) {
		
	/*	StringBuffer sql= new  StringBuffer();
		sql.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_getUserByid"));
		List<UserInfo> list =jdbcTemplate.query(sql.toString() ,new Object[]{aid},new BeanPropertyRowMapper(UserInfo .class));
		return list!=null?list.get(0):null;*/
		return selectOne(MAPPER_NAMESAPCEINFO+"selectByUid", info);
	}


	@Override
	public int saveUserByid(UserInfo user) {
		/*StringBuffer sql = new StringBuffer();
		sql.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_saveUserByid"));
		int result =  jdbcTemplate.update(sql.toString(), new Object[] {
			user.getName(),user.getSex(),user.getBirthday(),user.getCardid(),user.getAddress(),user.getEducation(),
			user.getCurrentaddr(),user.getPhone(),user.getDeptid(),user.getAid()
		});
		return result;*/
		return update(MAPPER_NAMESAPCEINFO+"updateInfo", user);
	}

 

	@Override
	public int deletecalling(Long uid) {
		/*StringBuffer sql = new StringBuffer();
		sql.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_deletecalling"));
		int result =  jdbcTemplate.update(sql.toString(), new Object[] {
			uid
		});
		return result;*/
		return update(MAPPER_NAMESAPCE+"delCalling", uid);
	}

	@Override
	public int insertcalling(Long uid, int callingid) {
		Map<String,Object> map = new HashMap();
		map.put("uid", uid);
		map.put("callingid", callingid);
		/*StringBuffer sql = new StringBuffer();
		sql.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_insertcalling"));
		int result =  jdbcTemplate.update(sql.toString(), new Object[] {
			uid,callingid
		});
		return result;*/
		return insert(MAPPER_NAMESAPCE+"insertUserCalling", map);
	}

	@Override
	public List<Integer> getCallingByuid(Long uid) {
		/*StringBuffer sql = new StringBuffer();
		sql.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_getCallingByuid"));
		List<Integer> list =this.jdbcTemplate.queryForList(sql.toString(), new Object[]{uid}, Integer.class);
		return list;*/
		return selectList(MAPPER_NAMESAPCE+"selectUserCalling", uid); 
	}
 
	@Override
	public List<UserInfo> getUserInfoListByDeptid(UserInfo dept) {
		/*StringBuffer sql = new StringBuffer();
		sql.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_getCallingListByDeptid"));
		List<UserInfo> list =jdbcTemplate.query(sql.toString() ,new Object[]{deptid},new BeanPropertyRowMapper(UserInfo .class));
		return list;*/
		return selectList(MAPPER_NAMESAPCEINFO+"selectList",dept);
	}

	@Override
	public int updateUserState(UserAccount useraccount) {
		/*StringBuffer sql = new StringBuffer();
		sql.append(XmlReader.readXml(Constants.SQLFileName,"UserDao_updateUserState"));
		int result = jdbcTemplate.update(sql.toString(),new Object[]{
			useraccount.getState(),useraccount.getId()
		});
		return result;*/
		return update(MAPPER_NAMESAPCE+"updateUserState", useraccount);
	}

	@Override
	public List<UserInfo> getUserAll(UserInfo u) {
		return selectList(MAPPER_NAMESAPCEINFO+"selectImUserInfo",  u);
	}

	@Override
	public int deleteUser(UserAccount ua) {
		Long id = ua.getId();
		update(MAPPER_NAMESAPCE+"deleteuser", id);
		update(MAPPER_NAMESAPCEINFO+"deleteuserinfo", id);
		return 1;
	}


}
